using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; 
namespace HermesPortal.Core.DAO
{
    class CovenantDAO
    {
        internal static Covenant GetCovenant(int id)
        {
            SqlDataReader reader = null;
            Covenant covenant = null;
            SqlConnection conn = new SqlConnection(  
                ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString );
            
            using (SqlCommand cmd = new SqlCommand("CovenantGet", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;   
                cmd.Parameters.AddWithValue("@CovenantID", id); 
                cmd.Connection.Open();
                
                reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    covenant = LoadCovenant(reader);
                }

                cmd.Connection.Close();  
            }
            return covenant;
        }


      
        private static Covenant LoadCovenant(SqlDataReader reader)
        {
            Covenant covenant = new Covenant(); 
            if (reader != null && !reader.IsClosed)
            {
                covenant.Id  = (int) reader["CovenantID"];
                if (reader["TemplateStyle"] != DBNull.Value  ) covenant.TemplateStyle =(int) reader["TemplateStyle"];
                if (reader["Name"]!= DBNull.Value  ) covenant.Name = (string)reader["Name"];
                if (reader["TagLine"]!= DBNull.Value  ) covenant.TagLine = (string)reader["TagLine"];
                if (reader["Header"]!= DBNull.Value   ) covenant.Header = (string)reader["Header"];
                if (reader["CurrentAffairs"] != DBNull.Value) covenant.CurrentAffairs = (string)reader["CurrentAffairs"];
                if (reader["SagaSummary"]!= DBNull.Value   ) covenant.SagaSummary = (string)reader["SagaSummary"];
                if (reader["FormationStory"]!= DBNull.Value   ) covenant.FormationStory = (string)reader["FormationStory"];
                if (reader["FoundedDate"] != DBNull.Value) covenant.FoundedDate = (string)reader["FoundedDate"];
                if (reader["CurrentYear"]!= DBNull.Value  ) covenant.CurrentYear = (int)reader["CurrentYear"];
            }
            return covenant; 
        }
    }

   
}
